package com.yc.thread;

import java.util.concurrent.atomic.AtomicInteger;

public class ZeroToThousand {

    public static void main(String[] args) throws InterruptedException {
        int count = 1000;
        MyRunnable myRunnable = new MyRunnable();
        for (int i = 0; i < count; i++) {
            new Thread(myRunnable, "Thread" + String.valueOf(i)).start();
        }
    }

    static class MyRunnable implements Runnable {

//        private AtomicInteger atomicInteger = new AtomicInteger(-1);
        private Integer integer = new Integer(-1);

        @Override
        public void run() {
            while(true) {
//                synchronized(atomicInteger) {
//                    if(atomicInteger.get() >= 999) {
//                        return;
//                    } else {
//                        atomicInteger.addAndGet(1);
//                    }
//                    System.out.println(atomicInteger.get());
//                }
                synchronized(integer) {
                    if(integer >= 999) {
                        return;
                    } else {
                        integer = integer + 1;
                    }
                    System.out.println(integer);
                }
//                try {
//                    Thread.sleep(1);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
            }
        }
    }

}
